package com.fau.fm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.fm.action.PublicAction;
import com.fau.fm.entity.MedicalAdmin;
import com.fau.fm.entity.MedicalStaffBasic;
import com.fau.fm.util.SystemConfig;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 员工薪资Action
 * @author liy
 * @datetime 2014年11月14日,下午4:59:14
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class StaffSalaryAction extends PublicAction implements ModelDriven<MedicalStaffBasic>{

	private static final long serialVersionUID = 8261619423805176907L;
	
	private MedicalStaffBasic staffSalary = new MedicalStaffBasic();
	
	/**
	 * @desc 所有请假类别
	 * @return null
	 * @author liy
	 * @datetime 2014年11月18日,上午11:26:03
	 */
	@Action(value = "leave_type")
	public String leaveType(){
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.list = this.baseAllService.getDictionaryManagerService().findKeyValueList(admin, SystemConfig.HM_SYSTEM_TYPE, staffSalary.getTypeName());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.list);
		return null;
	}
	
	/**
	 * @desc 分页查询请假扣款
	 * @return null
	 * @author liy
	 * @datetime 2014年11月18日,上午10:16:16
	 */
	@Action(value = "leave_deduct_page")
	public String leaveDeductPage(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getSalaryService().findLeaveDeductByPage(this.map, admin.getEnterpriseId());
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 进入请假扣款添加页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月18日,上午9:42:16
	 */
	@Action(value = "leave_deduct_add", results = {@Result(location = "leave_deduct_add.jsp")})
	public String leaveDeductAdd(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入请假扣款修改页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月18日,下午8:11:48
	 */
	@Action(value = "leave_deduct_edit", results = {@Result(location = "leave_deduct_edit.jsp")})
	public String leaveDeductEdit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入请假扣款设置页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月18日,上午9:29:18
	 */
	@Action(value = "leave_deduct_init", results = {@Result(location = "leave_deduct_init.jsp")})
	public String leaveDeductInit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入员工基本工资修改页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月17日,下午7:07:54
	 */
	@Action(value = "update_basic_salary", results = {@Result(location = "update_basic_salary.jsp")})
	public String updateBasicSalary(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入员工薪资管理页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午5:00:21
	 */
	@Action(value = "staff_salary_init", results = {@Result(location = "staff_salary_init.jsp")})
	public String staffSalaryInit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 获得公司的相关员工
	 * @return null
	 * @author liy
	 * @datetime 2014年11月15日,下午7:03:55
	 */
	@Action(value = "staff_page")
	public String staffPage(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getUserService().findStaffBasicByPage(map, admin.getEnterpriseId(), staffSalary.getName());
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 进入员工基本工资设定页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月17日,上午11:13:56
	 */
	@Action(value = "set_basic_salary", results = {@Result(location = "set_basic_salary.jsp")})
	public String setBasicSalary(){
		return SUCCESS;
	}
	
	/**
	 * @desc 修改时判断选择的类别是否已经存在
	 * @return null
	 * @author liy
	 * @datetime 2014年11月18日,下午9:19:54
	 */
	@Action(value = "leave_type_check")
	public String leaveTypeCheck(){
		boolean flag = this.baseAllService.getSalaryService().findLeaveTypeById(staffSalary.getId());
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		this.map.put("flag", flag);
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 保存员工基本工资
	 * @return null
	 * @author liy
	 * @datetime 2014年11月17日,上午11:54:04
	 */
	@Action(value = "staff_basic_save")
	public String staffBasicSave(){
		boolean flag = this.baseAllService.getUserService().insertStaffBasicSalary(staffSalary);
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(flag) {
			this.map.put("success", true);
		} else {
			this.map.put("success", false);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 保存请假扣款
	 * @return null
	 * @author liy
	 * @datetime 2014年11月18日,下午7:31:49
	 */
	@Action(value = "deduct_save")
	public String deductSave(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String typeId = request.getParameter("typeId");
		String deduct = request.getParameter("deduct");
		boolean flag = false;
		if(StringUtils.isNotBlank(typeId) && StringUtils.isNotBlank(deduct)){
			flag = this.baseAllService.getSalaryService().insertLeaveDeduct(typeId,deduct);
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(flag) {
			this.map.put("success", true);
		} else {
			this.map.put("success", false);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 更新员工基本工资
	 * @return null
	 * @author liy
	 * @datetime 2014年11月18日,上午9:02:47
	 */
	@Action(value = "staff_basic_update")
	public String staffBasicUpdate(){
		boolean flag = this.baseAllService.getUserService().updateStaffBasicSalary(staffSalary);
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(flag) {
			this.map.put("success", true);
		} else {
			this.map.put("success", false);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 更新请假扣款
	 * @return String
	 * @author liy
	 * @datetime 2014年11月18日,下午8:15:05
	 */
	@Action(value = "deduct_update")
	public String deductUpdate(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String typeId = request.getParameter("typeId");
		String deduct = request.getParameter("deduct");
		boolean flag = this.baseAllService.getSalaryService().updateLeaveDeduct(staffSalary.getId(), typeId, deduct);
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(flag) {
			this.map.put("success", true);
		} else {
			this.map.put("success", false);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 根据部门职位查询员工
	 * @return null
	 * @author liy
	 * @datetime 2014年11月17日,下午3:36:19
	 */
	@Action(value = "staff_specific_staff")
	public String specificStaff(){
		this.list = this.baseAllService.getUserService().findSpecificStaff(staffSalary.getDept(),staffSalary.getPosition());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.list);
		return null;
	}
	
	/**
	 * @desc 删除选择的基本工资记录
	 * @return null
	 * @author liy
	 * @datetime 2014年11月17日,下午6:00:03
	 */
	@Action(value = "staff_delete")
	public String staffDelete(){
		this.baseAllService.getUserService().deleteStaffBasic(this.staffSalary.getId());
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		this.map.put("success", true);
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 删除选择的请假扣款设置
	 * @return null
	 * @author liy
	 * @datetime 2014年11月18日,下午7:57:57
	 */
	@Action(value = "deduct_delete")
	public String deductDelete(){
		this.baseAllService.getSalaryService().deleteLeaveDeduct(this.staffSalary.getId());
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		this.map.put("success", true);
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 判断数据是否已存在
	 * @return null
	 * @author dangjh
	 * @datetime 2014年12月5日 下午3:38:53
	 */
	@Action(value = "include_staff_user")
	public String includeUser() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		boolean flag = this.baseAllService.getUserService().includeUserBasicSalary(staffSalary.getUid());
		if(flag) {
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询企业下所有的中心
	 * @return null
	 * @author dangjh
	 * 2014年12月30日 下午4:16:41
	 */
	@Action(value = "platform_view_list")
	public String platformView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String type = request.getParameter("type");
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getUserService().findByEidAll(admin.getEnterpriseId(), type);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询中心下所有的组
	 * @return null
	 * @author dangjh
	 * 2014年12月30日 下午4:16:41
	 */
	@Action(value = "group_view_list")
	public String groupView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getUserService().findByPlatformIdAll(staffSalary.getPlatformId());
		formatJSON(response, list);
		return null;
	}
	
	@Override
	public MedicalStaffBasic getModel() {
		return staffSalary;
	}

}
